var GetInfo = {

	typeChange : function(thiz, records, opts) {
		// alert(records[0].data.field1);
//		var cpuData = [['capacity.entitlement','capacity.entitlement'],
//	               ['wait','wait'], ['run','run'], ['costop','costop'], ['used','used'], ['ready','ready'],
//	               ['cpuentitlement','cpuentitlement'], ['swapwait','swapwait'], ['utilization','utilization'], ['latency','latency'], ['coreUtilization','coreUtilization'],
//	               ['capacity.provisioned','capacity.provisioned'], ['totalmhz','totalmhz'], ['entitlement','entitlement'], ['demand','demand'], ['maxlimited','maxlimited'],
//	               ['corecount.provisioned','corecount.provisioned'], ['overlap','overlap'], ['corecount.contention','corecount.contention'],
//	               ['corecount.usage','corecount.usage'], ['capacity.usage','capacity.usage'], ['usagemhz','usagemhz'], ['capacity.demand','capacity.demand'], ['idle','idle'],
//	               ['totalCapacity','totalCapacity'], ['system','system'], ['reservedCapacity','reservedCapacity'], ['usage','usage'],
//	               ['capacity.contention','capacity.contention']];
		var cpuData =[['usage','usage'], ['run','run'], ['costop','costop'], ['used','used'], ['ready','ready']];
		var memoryData =[['usage','usage'],['sysUsage','sysUsage'],['state','state']];
		var diskData =[['usage','usage'],['used','used'],['capacity','capacity']];
		Ext.getCmp("counterName").setValue("");
		switch(records[0].data.field1)
		{
		case "cpu":
			Ext.getCmp("counterName").getStore().loadData(cpuData);
			break;
		case "memory":
			Ext.getCmp("counterName").getStore().loadData(memoryData);
			break;
		case "disk":
			Ext.getCmp("counterName").getStore().loadData(diskData);
			break;
		}
	},
	realtimeSearch : function() {
		var counterName = Ext.getCmp("counterName").getValue();
		var interval = Ext.getCmp("interval").getValue();
		Ext.getCmp("chartPanel").getStore().removeAll();
		var msg = this.chackRelValue(counterName, interval);
		if (msg != "") {
			Ext.Msg.alert("提示", msg);
			return;
		}
		switch (Ext.getCmp("type").getValue()) {
		case "cpu":
			GetInfo.getRealtimeInfo("getRealtimeCpuInfo", vmname,
					counterName, interval);
			break;
		case "memory":
			GetInfo.getRealtimeInfo("getRealtimeMemInfo", vmname,
					counterName, interval);
			break;
		case "disk":
			GetInfo.getRealtimeInfo("getRealtimeDiskInfo", vmname,
					counterName, interval);
			break;
		}
		var info = "主机：" + vmname.split('(')[0] + ",类型：" + Ext.getCmp("type").getValue()
				+ ",参数：" + counterName + ",取点间隔：" + interval + "秒";
		Ext.getCmp("panel1").setTitle(info + "的实时信息");

	},

	chackRelValue : function(counterName, interval) {
		if (counterName == "")
			return "计数参数不能为空";
		if (interval == "")
			return "取点间隔不能为空";
		return "";
	},

	historySearch : function() {
		var starttime = Ext.getCmp("starttime").getValue();
		var counterName = Ext.getCmp("counterName").getValue();
		var duration = Ext.getCmp("duration").getValue();
		var interval = Ext.getCmp("interval").getValue();
		Ext.getCmp("chartPanel").getStore().removeAll();
		var msg = this.chackHisValue(starttime, counterName,duration,interval);
		if (msg != "") {
			Ext.Msg.alert("提示", msg);
			return;
		}
		switch (Ext.getCmp("type").getValue()) {
		case "cpu":
			GetInfo.getHistoryInfo("getHistoryCpuInfo", vmname, starttime,
					counterName, duration, interval);
			break;
		case "memory":
			GetInfo.getHistoryInfo("getHistoryMemInfo", vmname, starttime,
					counterName, duration, interval);
			break;
		case "disk":
			GetInfo.getHistoryInfo("getHistoryDiskInfo", vmname, starttime,
					counterName, duration, interval);
			break;
		}
		var info = "主机：" + vmname.split('(')[0] + ",类型：" + Ext.getCmp("type").getValue()
				+ ",参数" + counterName + ",查询区间：" + duration + "分" + "，前"
				+ starttime + "分取点"
		Ext.getCmp("panel1").setTitle(info + "的历史信息");
	},
	
	chackHisValue : function(starttime, counterName,duration,interval) {
		if (starttime == "")
			return "开始时间不能为空";
		if (counterName == "")
			return "计数参数不能为空";
		if (duration == "")
			return "持续时间不能为空";
		if (interval == "")
			return "取点间隔不能为空";
		if(duration>starttime)
		{
			return "持续时间有误，持续时间不能大于开始时间";
		}
		return "";
	},

	getHistoryInfo : function(handleName, vmname, starttime, counterName,
			duration, interval) {
		var query = new Query('HistoryResourcesMonitor',
				'HistoryResourcesMonitor');
		query.setHandlerMethod(handleName);
		query.addCondition("vmname", vmname);
		query.addCondition("starttime", starttime);
		query.addCondition("counterName", counterName);
		query.addCondition("duration", duration);
		query.addCondition("interval", interval);
		query.setCompletedFun(GetInfo.afterloadInfo);
		query.setCompletedFunScope(GetInfo);
		query.load();
	},

	getRealtimeInfo : function(handleName, vmname, counterName, interval) {
		var query = new Query('RealtimeResourcesMonitor',
				'RealtimeResourcesMonitor');
		query.setHandlerMethod(handleName);
		query.addCondition("vmname", vmname);
		query.addCondition("counterName", counterName);
		query.addCondition("interval", interval);
		query.setCompletedFun(GetInfo.afterloadInfo);
		query.setCompletedFunScope(GetInfo);
		query.load();
	},

	afterloadInfo : function(success, dataset, response, opts) {
		
		if (success) {
			if (dataset && !Ext.isEmpty(dataset.record)) {
				this.dataset = dataset;
				Ext.getCmp("chartPanel").getStore().add(dataset.record);
			}else{
				Ext.Msg.alert("提示","查询不到监控数据");
			}
		} else {
			
		}
	}
};
